package tongwentongshu.com.app.modelim;

        import android.content.Context;
        import android.util.Log;

        import com.tencent.imsdk.TIMConversation;
        import com.tencent.imsdk.TIMConversationType;
        import com.tencent.imsdk.TIMFriendshipManager;
        import com.tencent.imsdk.TIMUserProfile;
        import com.tencent.imsdk.TIMValueCallBack;
        import com.tencent.imsdk.ext.message.TIMConversationExt;

        import java.util.ArrayList;
        import java.util.List;

        import tongwentongshu.com.app.App;
        import tongwentongshu.com.app.R;
        import tongwentongshu.com.app.activityim.ChatActivity;


/**
 * 好友或群聊的会话
 */
public class NomalConversation extends Conversation {


    private TIMConversation conversation;



    //最后一条消息
    private Message lastMessage;

    private String imageUrl ;


    public NomalConversation(TIMConversation conversation ){
        this.conversation = conversation;
        type = conversation.getType();
        identify = conversation.getPeer();


        List<String> users = new ArrayList<String>();
        users.add(identify);
        //获取好友资料
        TIMFriendshipManager.getInstance().getUsersProfile(users, new TIMValueCallBack<List<TIMUserProfile>>(){
            @Override
            public void onError(int code, String desc){
                //错误码code和错误描述desc，可用于定位请求失败原因
                //错误码code列表请参见错误码表
                Log.e("shi", "getFriendsProfile failed: " + code + " desc");
            }

            @Override
            public void onSuccess(List<TIMUserProfile> result){

                name = result.get(0).getNickName().isEmpty() ? result.get(0).getIdentifier(): result.get(0).getNickName();

                imageUrl =result.get(0).getFaceUrl().isEmpty() ? "" : result.get(0).getFaceUrl();

                Log.e("shi", "getFriendsProfile succ");
                for(TIMUserProfile res : result){

                    Log.e("caimeneg", "identifier: " + res.getIdentifier() + " nickName: " + res.getNickName()
                            + " remark: " + res.getRemark());
                }
            }
        });














    }


    public void setLastMessage(Message lastMessage) {
        this.lastMessage = lastMessage;
    }


    @Override
    public String getAvatar() {
        switch (type){
            case C2C:
                System.out.println("我是返回的头像:" + imageUrl);
                return imageUrl;
            case Group:
                return imageUrl;
        }
        return  "";

       /*
        switch (type){
            case C2C:
                System.out.println("我是返回的头像:" + imageUrl);
                return R.drawable.head_other;
            case Group:
                return R.drawable.head_group;
        }
        return 0;*/
    }

    /**
     * 跳转到聊天界面或会话详情
     *
     * @param context 跳转上下文
     */
    @Override
    public void navToDetail(Context context) {

        ChatActivity.navToChat(context,identify,type , imageUrl , name);
    }

    /**
     * 获取最后一条消息摘要
     */
    @Override
    public String getLastMessageSummary(){
        TIMConversationExt ext = new TIMConversationExt(conversation);
        if (ext.hasDraft()){
            TextMessage textMessage = new TextMessage(ext.getDraft());
            if (lastMessage == null || lastMessage.getMessage().timestamp() < ext.getDraft().getTimestamp()){
                return App.getContext().getString(R.string.conversation_draft) + textMessage.getSummary();
            }else{
                return lastMessage.getSummary();
            }
        }else{
            if (lastMessage == null) return "";
            return lastMessage.getSummary();
        }
    }

    /**
     * 获取名称
     */
    @Override
    public String getName() {
        if (type == TIMConversationType.Group){
//            name=GroupInfo.getInstance().getGroupName(identify);
//            if (name.equals("")) name = identify;
        }else{


//            FriendProfile profile = FriendshipInfo.getInstance().getProfile(identify);
//            name=profile == null?identify:profile.getName();
        }
        return name;
    }

    @Override
    public String getHeadUrl() {
        return null;
    }


    /**
     * 获取未读消息数量
     */
    @Override
    public long getUnreadNum(){
        if (conversation == null) return 0;
        TIMConversationExt ext = new TIMConversationExt(conversation);
        return ext.getUnreadMessageNum();
    }

    /**
     * 将所有消息标记为已读
     */
    @Override
    public void readAllMessage(){
        if (conversation != null){
            TIMConversationExt ext = new TIMConversationExt(conversation);
            ext.setReadMessage(null, null);
        }
    }


    /**
     * 获取最后一条消息的时间
     */
    @Override
    public long getLastMessageTime() {
        TIMConversationExt ext = new TIMConversationExt(conversation);
        if (ext.hasDraft()){
            if (lastMessage == null || lastMessage.getMessage().timestamp() < ext.getDraft().getTimestamp()){
                return ext.getDraft().getTimestamp();
            }else{
                return lastMessage.getMessage().timestamp();
            }
        }
        if (lastMessage == null) return 0;
        return lastMessage.getMessage().timestamp();
    }

    /**
     * 获取会话类型
     */
    public TIMConversationType getType(){
        return conversation.getType();
    }
}
